﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using System.Net.Mail;

namespace DataAccess
{
    public class CartRepository : ConnectionClass
    {

        public CartRepository() : base(){}

        public void AddtoCart(string UserName,int id,int quantity)
        {
            Cart sc = new Cart();
            sc.ProductID = id;
            sc.Quantity = quantity;
            sc.UserName = UserName;

            db.AddToCarts(sc);
            db.SaveChanges();

        }

        public Cart FindCartByID(int id)
        {
            return db.Carts.SingleOrDefault(c => c.ID == id);
        }

        public void RemoveProductFromCart(int id)
        {
            db.DeleteObject(FindCartByID(id));
            db.SaveChanges();
        }

        public IEnumerable<Cart> GetUserNameCart(string username)
        {
           return db.Carts.Where(c => c.UserName == username);
        }

        public void UpdateCart(Cart c)
        {
            Cart oc = FindCartByID(c.ID);
            db.Carts.Attach(oc);
            db.Carts.ApplyCurrentValues(c);
            db.SaveChanges();
        }

        public void DeleteCart(int id)
        {
            db.Carts.DeleteObject(FindCartByID(id));
        }

        public int GetNoOfItemsInShoppingCart(string username)
        {
            if (db.Carts.Where(s => s.UserName == username).Count() == 0)
                return 0;
            else return db.Carts.Where(S => S.UserName == username).Count();
        }
    }
}
